<?php
namespace App\Http\Controllers\Business\App\H5;

use App\Models\City;
use App\Models\Order;
use App\Models\ShopActivity;
use App\Models\ShopCoupon;
use App\Models\User;
use App\Models\Shop;

class H5
{
    //用户消费记录
    public static function getOrder() {
        $data = Order::where('orders.status', '1')
            ->join('users', 'orders.userid', '=', 'users.id')
            ->where('orders.actualamount', '>=', 10)
            ->select('orders.actualamount', 'orders.shopname', 'users.mobile')
            ->orderBy('orders.created_at', 'desc')
            ->paginate(100);
        if ($data) {
            foreach ($data as $k => $v) {
                $mobile = str_replace(substr($v['mobile'], 3, 4), '****', $v['mobile']);
                $data[$k]['mobile'] = $mobile;
            }
        }
        return ['status'=>'success', 'data'=>$data ];
    }
    //新增，领取商家券，如光之乳酪
    public static function addShopCoupon($userId, $shopId, $activityId) {
        if(!$userId || !$shopId || !$activityId) {
            return ['status'=>'failed', 'message'=>'领取失败'];
        }
        $shop = Shop::where('id', $shopId)->first();
        if(!$shop) {
            return ['status'=>'failed', 'message'=>'领取失败'];
        }
        $shopActivity = ShopActivity::where('shopid', $shopId)->where('id', $activityId)->where('status', 1)->where('issend', 1)->first();
        if(!$shopActivity) {
            return ['status'=>'failed', 'message'=>'活动已结束了'];
        }
        if(ShopCoupon::where('shopid', $shopId)->where('activityid', $activityId)->count() > $shopActivity->maxnum ) {
            return ['status'=>'failed', 'message'=>'已领取完了'];
        }
        $oldShopCoupon = ShopCoupon::where('shopid', $shopId)->where('userid', $userId)->where('activityid', $activityId)->first();
        if($oldShopCoupon) {
            $returnData = ['type'=>'alreadyget', 'message'=>'已领取过', 'couponnumber'=>'', 'endtime'=>''];
            return ['status'=>'success', 'data'=>$returnData];
        }
        $couponNumber = $userId . $shopId . date('ymdhis');
        $shopCoupon = ShopCoupon::create([
            'userid' => $userId,
            'shopid' => $shopId,
            'activityid' => $activityId,
            'couponnumber' => $couponNumber,
            'endtime' => $shopActivity->endtime,
            'status' => 0
        ]);
        $shopCoupon->save();
        $returnData = ['type'=>'firstget', 'message'=>'领取成功', 'couponnumber'=>$couponNumber];
        return ['status'=>'success', 'data'=>$returnData];
    }
    //获取商家券
    public static function getShopCoupon($couponNumber) {
        if(!$couponNumber) {
            return ['status'=>'failed', 'message'=>'获取券信息失败'];
        }
        $shopCoupon = ShopCoupon::where('couponnumber', $couponNumber)->first();
        if(!$shopCoupon) {
            return ['status'=>'failed', 'message'=>'获取券信息失败'];
        }
        return ['status'=>'success', 'data'=>$shopCoupon];
    }
    //使用商家券
    public static function useShopCoupon($couponNumber, $password) {
        if(!$couponNumber||!$password) {
            return ['status'=>'failed', 'message'=>'验证失败'];
        }
        $shopCoupon = ShopCoupon::where('couponnumber', $couponNumber)->first();
        if(!$shopCoupon) {
            return ['status'=>'failed', 'message'=>'验证失败'];
        }
        if($shopCoupon->status != 0) {
            return ['status'=>'failed', 'message'=>'已使用过了'];
        }
        $shopId = $shopCoupon->shopid;
        $activityId = $shopCoupon->activityid;
        $shopActivity = ShopActivity::where('shopid', $shopId)->where('activityid', $activityId)->where('status', 1)->first();
        if(!$shopActivity) {
            return ['status'=>'failed', 'message'=>'验证失败'];
        }
        if($shopActivity->password == md5($password)) {
            $shopCoupon->usetime = date('Y-m-d H:i:s');
            $shopCoupon->status = 1;
            $shopCoupon->save();
            return ['status'=>'success', 'message'=>'验证成功'];
        } else {
            return ['status'=>'failed', 'message'=>'校验密码错误'];
        }
    }
}